
from apps.user_operation.models import Banner
from apps.xiaChuFang.models import ActivityCategroy,Foods
from apps.user_operation.serializers import bannerSerializers
from apps.xiaChuFang.serializers import foodserializer


from utils import Json
from decorator.functools import check_userToken_timeout,request_method

from rest_framework.views import APIView
from django.views.decorators.csrf import csrf_exempt

class xcfAPi(APIView):

    """
    首页数据
    """
    @csrf_exempt
    @request_method
    def homelist(request):

        # 获取轮播数据
        bannerlist = []
        for banner in Banner.objects.all():
            bannerser =  bannerSerializers(banner)
            bannerlist.append(bannerser.data)

        # 新秀菜谱
        newcategroy =  ActivityCategroy.objects.get(title="新秀菜谱")
        newFoods = Foods.objects.filter(foodId=newcategroy.categoryId)[:10]
        newfoodlist = []
        for food in newFoods:
            foodser = foodserializer(food)
            newfoodlist.append(foodser.data)

        # 最近流行
        popularcategroy = ActivityCategroy.objects.get(title="最新创建")
        popularFoods = Foods.objects.filter(foodId=popularcategroy.categoryId)[:10]
        popularfoodlist = []
        for food in popularFoods:
            foodser = foodserializer(food)
            popularfoodlist.append(foodser.data)

        return Json.json_response({
            "banner":bannerlist,
            "newfoodlist":newfoodlist,
            "popularfoodlist":popularfoodlist
        })